package com.wzy.mall.service.admin.impl;

import com.wzy.mall.mapper.adminMapper.AdminMapper;
import com.wzy.mall.model.bo.adminBO.AdminBO;
import com.wzy.mall.model.bo.adminBO.AdminLoginBO;
import com.wzy.mall.model.bo.adminBO.AdminPwdBO;
import com.wzy.mall.model.pojo.adminPOJO.AdminPOJO;
import com.wzy.mall.service.admin.AdminService;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminServiceImpl implements AdminService {

    public Integer selectAdminByUsernameAndPassword(AdminLoginBO adminLoginBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        // 将loginBO转换为POJO的Admin对象
        AdminPOJO LoginAdmin = new AdminPOJO();
        LoginAdmin.setUsername(adminLoginBO.getEmail());
        LoginAdmin.setPassword(adminLoginBO.getPwd());

        try {
            Integer result = mapper.selectAdminByUsernameAndPassword(LoginAdmin);
            //System.out.println(result);
            if(result == null || result == 0) {
                return 404;
            } else {
                // 账号匹配，根据id查找昵称
                String nickname = mapper.selectNickNameByID(result);
                // 将昵称放入BO对象中
                adminLoginBO.setNickName(nickname);
                return 200;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return 500;
    }

    public List<AdminPOJO> selectAllAdmins() {
        System.out.println("----1---");
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        System.out.println("----------2----------");
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminPOJO> adminPOJOList = null;
        try {
            adminPOJOList = mapper.selectAllAdmins();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return adminPOJOList;
    }

    public int addAdmin(AdminBO adminBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        AdminPOJO adminPOJO = new AdminPOJO();
        adminPOJO.setUsername(adminBO.getEmail());
        adminPOJO.setPassword(adminBO.getPwd());
        adminPOJO.setNickname(adminBO.getNickname());

        int result = 0;
        try {
            result = mapper.addAdmin(adminPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public int deleteAdmin(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        int result = 0;
        try {
            result = mapper.deleteAdmin(id);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return result;
    }

    public List<AdminPOJO> selectByCondition(AdminBO adminBO) {
        // 将BO对象转换为POJO对象
        AdminPOJO adminPOJO = new AdminPOJO();
        adminPOJO.setUsername(adminBO.getEmail());
        adminPOJO.setNickname(adminBO.getNickname());
        //System.out.println(adminPOJO);

        // 与数据库交互查询
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminPOJO> adminPOJOList = null;
        try {
            adminPOJOList = mapper.selectByCondition(adminPOJO);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return adminPOJOList;
    }

    public AdminPOJO selectById(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        AdminPOJO adminPOJO = null;
        try {
            adminPOJO = mapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return adminPOJO;
    }

    public int updateById(AdminBO adminBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        // 将BO对象转换为POJO对象
        AdminPOJO adminPOJO = new AdminPOJO();
        adminPOJO.setId(adminBO.getId());
        adminPOJO.setUsername(adminBO.getEmail());
        adminPOJO.setNickname(adminBO.getNickname());
        adminPOJO.setPassword(adminBO.getPwd());
        int result = 0;
        try {
            result = mapper.updateById(adminPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return result;
    }

    public int changePwd(AdminPwdBO adminPwdBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);

        // 首先需要确认旧密码是否一致
        String nickname = adminPwdBO.getAdminToken();
        AdminPOJO adminPOJO = mapper.selectPwdByNickname(nickname);
        if(adminPOJO == null) {
            return 404;
        }
        if(!adminPOJO.getPassword().equals(adminPwdBO.getOldPwd())) {
            return 401;
        }

        // 密码校验完成，进行修改操作
        adminPOJO.setPassword(adminPwdBO.getNewPwd());
        int result = 0;
        try {
            result = mapper.updatePwdById(adminPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        if(result == 0) {
            return 500;
        } else {
            return 200;
        }

    }


}
